//
//  JSSOriginalStatusView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/21.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
import SnapKit
import YYText


class JSSOriginalStatusView: UIView {
    
    var bottomContrait: Constraint?
    
    var statusViewModel:JSSStatusViewModel? {
        
        didSet{
            //进行赋值操作
            //设置头像
            iconImageView.sd_setImageWithURL(NSURL(string: (statusViewModel?.statusModel?.user?.profile_image_url)!), placeholderImage: UIImage(named: "common_icon_membership"))
            
           //设置昵称
            nameLable.text = statusViewModel?.statusModel?.user?.name
            
            //设置vipImage
            vipImageView.image = statusViewModel?.vipImage
            
            //设置创建时间
//            createdAtTime.text = statusViewModel?.statusModel?.created_at
            createdAtTime.text = statusViewModel?.createTimeText
            
            //设置微博来源
//            source.text = statusViewModel?.statusModel?.source
            source.text = statusViewModel?.sourceContent
            
            //设置认证
//            verifiedImageView.image =
            
            //设置正文内容
//            contentLable.text = statusViewModel?.statusModel?.text
            contentLable.attributedText = statusViewModel?.originalAttrString
            
            //判断之前将原先的约束失效
            bottomContrait?.uninstall()
            //判断是否有配图
            //如果有配图
            if let pics = statusViewModel?.statusModel?.pic_urls where pics.count > 0 {
                
                //判如果图片是一张图片 设置itemSize为配图的大小
                if pics.count == 1 {
                    //获取flowLayout
                    let flowLayout = pictureImageView.collectionViewLayout as? UICollectionViewFlowLayout
                    //更换collectionView的itemsize
                    flowLayout?.itemSize = statusViewModel!.originalStatusViewPictureSize
                    //print("一张图\(flowLayout?.itemSize)")
                    
                }else {
                    
                    //否则设置成为正方形 
                     let flowLayout = pictureImageView.collectionViewLayout as? UICollectionViewFlowLayout
                    flowLayout?.itemSize = CGSize(width: JSSPictureInfoViewItemWH, height: JSSPictureInfoViewItemWH)
                   // print("多张图\(flowLayout?.itemSize)")
                }
                
                
                //1.设置数据
                pictureImageView.pic_urls = pics
                
                pictureImageView.snp_updateConstraints(closure: { (make) -> Void in
                    
                    make.size.equalTo((statusViewModel?.originalStatusViewPictureSize)!)
                })
                //2.显示配图
                pictureImageView.hidden = false
                //3.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                   bottomContrait = make.bottom.equalTo(pictureImageView).constraint
                })
                
            }else{
            
                //如果没有配图
                //1.隐藏配图
                pictureImageView.hidden = true
                //2.更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                   bottomContrait = make.bottom.equalTo(contentLable).constraint
                })
            }
            
            
        }
    }

   override init(frame: CGRect) {
    super.init(frame: frame)
    
    setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        
        //设置原发微博的背景颜色
        self.backgroundColor = UIColor.whiteColor()
//        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        
        addSubview(iconImageView)
        addSubview(nameLable)
        addSubview(vipImageView)
        addSubview(createdAtTime)
        addSubview(source)
        addSubview(verifiedImageView)
        addSubview(contentLable)
        addSubview(pictureImageView)
        
        
        //iconImageView约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.equalTo(JSSStatusMargin)
            make.width.height.equalTo(JSSStatusIconImageW)
        }

        //nameLable约束
        nameLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView)
            make.leading.equalTo(iconImageView.snp_bottom).offset(JSSStatusMargin)
        }
        
        //vipImageView约束
        
    vipImageView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(nameLable)
        make.leading.equalTo(nameLable.snp_trailing).offset(JSSStatusMargin)
        
        }
        //createdAtTime约束
        createdAtTime.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.leading.equalTo(nameLable.snp_leading)
        }
        //source约束
        
        source.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(createdAtTime)
            make.leading.equalTo(createdAtTime.snp_trailing).offset(JSSStatusMargin)
        }
        //verifiedImageView认证约束
        verifiedImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconImageView.snp_trailing).offset(-2)
            make.centerY.equalTo(iconImageView.snp_bottom).offset(-2)
        }

        //正文约束
        contentLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconImageView.snp_bottom).offset(JSSStatusMargin)
            make.leading.equalTo(iconImageView)
            
        }
        //pictureImageView配图的约束
        pictureImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLable)
            make.top.equalTo(contentLable.snp_bottom).offset(JSSStatusMargin)
            make.size.equalTo(CGSizeMake(80, 80))
            
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            
           bottomContrait =  make.bottom.equalTo(pictureImageView.snp_bottom).offset(JSSStatusMargin).constraint
        }
      
    }
    
    //懒加载头像IconImageView
    private lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default"))
    
    //懒加载昵称lable
    private lazy var nameLable:UILabel = {
        let name = UILabel(textColor: UIColor.darkGrayColor(), textFont: JSSStatusNameFont)
        
        name.text = "昵称"
        return name
        
    }()
    
    //懒加载vip
    private lazy var vipImageView:UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    
    //创建时间Lable
    private lazy var createdAtTime :UILabel = {
        
        let time = UILabel(textColor: UIColor.orangeColor(), textFont: JSSStatusTimeFone)
        time.text = "今天"
        
        return time
        
    }()
    
    //微博来源lable
    private lazy var source:UILabel = {
        
        let s = UILabel(textColor: UIColor.darkGrayColor(), textFont: JSSStatusSourceFone)
        
        s.text = "iphone6s"
        
        return s
        
    }()
    
    
    //认证图片
    private lazy var verifiedImageView:UIImageView = UIImageView(image:UIImage(named: "avatar_vip"))
    
    //正文lable
    private lazy var contentLable:YYLabel = {
    
      //  let lable = UILabel(textColor: UIColor.darkGrayColor(), textFont: JSSStatusContentFont, preferredMaxLayoutWidth: SCREENW - 2*JSSStatusMargin)
        
        let lable = YYLabel()
        lable.textColor = UIColor.darkGrayColor()
        lable.font = UIFont.systemFontOfSize(JSSStatusContentFont)
        lable.preferredMaxLayoutWidth = SCREENW - 2*JSSStatusMargin
        lable.numberOfLines = 0
        
        lable.userInteractionEnabled = true
    
        return lable
    
    }()
    
    //原创微博配图
    private lazy var pictureImageView:JSSPictureInfoView = JSSPictureInfoView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
}
